<?php

namespace app\admin\controller\hotel;

use app\common\controller\Backend;

class Coupon extends Backend
{
    // 当前模型
    protected $model = null;
 
    // 无需登陆
    protected $noNeedLogin = [];
    // 无需鉴权
    protected $noNeedRight = [];

    public function __construct(){
        parent::__construct();

        $this->CouponModel = model('Hotel.Coupon');
        $this->CouponListModel = model('Hotel.Couponlist');

        $StatusList = [
            '0' => '活动未开始',
            '1' => '活动中',
            '2' => '活动结束'
        ];

        $this->assign('StatusList',$StatusList);
    }


    // 优惠券首页
    public function index(){
        $this->request->filter(['strip_tags','trim']);
        if($this->request->isAjax()){
            list($where,$sort,$order,$offset,$limit) = $this->buildparams();

            // 表格两个返回值
            $total = $this->CouponModel
                ->where($where)
                ->order($sort,$order)
                ->count();
            
            $list = $this->CouponModel
                ->where($where)
                ->order($sort,$order)
                ->limit($offset,$limit)
                ->select();

            $result = [
                'total' => $total,
                'rows' => $list
            ];

            return json($result);
        }

        return $this->view->fetch();
    }


    // 添加
    public function add(){
        if($this->request->isPost()){
            // var_dump($this->request->param());
            // exit;

            $params = $this->request->param('row/a');
            // var_dump($params);
            // exit;
            $createtime = strtotime($params['createtime']);
            $endtime = strtotime($params['endtime']);
            // var_dump($createtime);
            // exit;

            $data = [
                'title' => $params['title'],
                'thumb' => $params['thumb'],
                'createtime' => $createtime,
                'endtime' => $endtime,
                'rate' => $params['rate'],
                'total' => $params['total'],
                'status' => $params['status'],
                'stock' => $params['stock']
            ];

            $result = $this->CouponModel->validate('common/hotel/coupon')->save($data);

            if($result === FALSE){
                $this->error('添加优惠券活动失败');
                exit;
            }else{
                $this->success('添加优惠券活动成功');
                exit;
            }
        }
        return $this->view->fetch();
    }

    // 修改
    public function edit($ids=NULL){
        $row = $this->CouponModel->find($ids);
        if(!$row){
            $this->error('当前数据不存在');
            exit;
        }

        if($this->request->isPost()){
            $params = $this->request->param('row/a');

            $createtime = strtotime($params['createtime']);
            $endtime = strtotime($params['endtime']);
            
            $data = [
                'id' => $ids,
                'title' => $params['title'],
                'thumb' => $params['thumb'],
                'createtime' => $createtime,
                'endtime' => $endtime,
                'rate' => $params['rate'],
                'total' => $params['total'],
                'status' => $params['status']
            ];

            $result = $this->CouponModel->isUpdate(true)->save($data);

            if($result === FALSE){
                $this->error('编辑优惠券活动失败');
                exit;
            }else{
                if($data['thumb'] != $row['thumb']){
                    is_file('.'.$row['thumb']) && @unlink('.'.$row['thumb']);
                }

                $this->success('编辑优惠券活动成功');
                exit;
            }
        }

        $this->assign('row',$row);
        return $this->view->fetch();
    }


    // 删除
    public function del($ids=NULL){
        $ids = $this->request->param('ids',[],'trim');
        $list = $this->CouponModel->where(['id' => ['IN',$ids]])->select();

        // var_dump($list);
        // exit;

        if(!$list){
            $this->error('活动不存在');
            exit;
        }

        // 图片去空
        $thumb = array_column($list,"thumb");

        // 去重、去空
        $thumb = array_filter($thumb);
        $thumb = array_unique($thumb);

        $result = $this->CouponModel->whereIn('id',$ids)->delete(true);

        if($result === FALSE){
            $this->error('删除失败');
            exit;
        }else{
            // 删除图片
            if(!empty($thumb)){
                foreach($thumb as $item){
                    is_file('.'.$item) && @unlink('.'.$item);
                }
            }
            $this->success('删除成功');
            exit;
        }
    }

    // 领取详情
    public function info($ids=NULL){
        // 查询优惠券是否存在
        $coupon = $this->CouponModel->find($ids);
        if(!$coupon){
            $this->error('优惠券信息未知');
            exit;
        }

        $this->request->filter(['strip_tags','trim']);

        if($this->request->isAjax()){
            list($where,$sort,$order,$offset,$limit) = $this->buildparams();

            $total = $this->CouponlistModel
                ->where($where)
                ->where(['cid' => $ids])
                ->order($sort,$order)
                ->count();

            $list = $this->CouponlistModel
                ->with(['coupon','business'])
                ->where($where)
                ->where(['cid' => $ids])
                ->order($sort,$order)
                ->limit($offset,$limit)
                ->select();

            $result = [
                'total' => $total,
                'rows' => $list
            ];

            return json($result);
        }

        return $this->view->fetch();
    }

}
